# This code reproduces the regression models #

Vals <- read.table(file.choose(), header = TRUE)

Vals$RankFree <- 5 - Vals$Q73_1
Vals$RankEq <- 5 - Vals$Q73_2
Vals$RankES <- 5 - Vals$Q73_3
Vals$RankMT <- 5 - Vals$Q73_4
Vals$RankLO <- 5 - Vals$Q73_5
table(Vals$RankFree)

# IDEOL 7-PT #
Vals$Ideol7 <- NA
Vals$Ideol7[Vals$Q36 == 1] <- 1
Vals$Ideol7[Vals$Q36 == 2] <- 2
Vals$Ideol7[Vals$Q36 == 3] <- 3
Vals$Ideol7[Vals$Q36 == 4] <- 4
Vals$Ideol7[Vals$Q36 == 5] <- 5
Vals$Ideol7[Vals$Q36 == 6] <- 6
Vals$Ideol7[Vals$Q36 == 7] <- 7

Vals$IdeolStrength <- NA
Vals$IdeolStrength[Vals$Ideol7 == 1] <- 3
Vals$IdeolStrength[Vals$Ideol7 == 2] <- 2
Vals$IdeolStrength[Vals$Ideol7 == 3] <- 1
Vals$IdeolStrength[Vals$Ideol7 == 4] <- 0
Vals$IdeolStrength[Vals$Ideol7 == 5] <- 1
Vals$IdeolStrength[Vals$Ideol7 == 6] <- 2
Vals$IdeolStrength[Vals$Ideol7 == 7] <- 3
Vals$IdeolStrength1 <- Vals$IdeolStrength/3

# PARTY #
Vals$Party <- NA
Vals$Party[Vals$Q38 == 1] <- 1
Vals$Party[Vals$Q38 == 2] <- 2
Vals$Party[Vals$Q40 == 1] <- 3
Vals$Party[Vals$Q40 == 3] <- 4
Vals$Party[Vals$Q40 == 2] <- 5
Vals$Party[Vals$Q39 == 2] <- 6
Vals$Party[Vals$Q39 == 1] <- 7

Vals$PartyStrength <- NA
Vals$PartyStrength[Vals$Party == 1] <- 3
Vals$PartyStrength[Vals$Party == 2] <- 2
Vals$PartyStrength[Vals$Party == 3] <- 1
Vals$PartyStrength[Vals$Party == 4] <- 0
Vals$PartyStrength[Vals$Party == 5] <- 1
Vals$PartyStrength[Vals$Party == 6] <- 2
Vals$PartyStrength[Vals$Party == 7] <- 3
Vals$PartyStrength1 <- Vals$PartyStrength/3


Vals$Lib <- ifelse(Vals$Q36 < 4, 1, 0)
table(Vals$Lib)

Vals$Con <- ifelse(Vals$Q36 > 4, 1, 0)
table(Vals$Con)

Vals$Mod <- ifelse(Vals$Q36 == 4, 1, 0)
table(Vals$Mod)

Vals$Dem <- ifelse(Vals$Party < 4, 1, 0)
Vals$Rep <- ifelse(Vals$Party > 4, 1, 0)
Vals$Ind <- ifelse(Vals$Party == 4, 1, 0)

Vals$SortedDem <- ifelse(Vals$Dem == 1 & Vals$Lib == 1, 1, 0)
table(Vals$SortedDem)
Vals$UnsortedDem <- ifelse(Vals$Dem == 1 & Vals$Lib == 0, 1, 0)
table(Vals$UnsortedDem)
Vals$SortedRep <- ifelse(Vals$Rep == 1 & Vals$Con == 1, 1, 0)
table(Vals$SortedRep)
Vals$UnsortedRep <- ifelse(Vals$Rep == 1 & Vals$Con == 0, 1, 0)
table(Vals$UnsortedRep)

Vals$SortedParty <- NA
Vals$SortedParty[Vals$SortedDem == 1] <- "SD"
Vals$SortedParty[Vals$UnsortedDem == 1] <- "UD"
Vals$SortedParty[Vals$Ind == 1] <- "I"
Vals$SortedParty[Vals$SortedRep == 1] <- "SR"
Vals$SortedParty[Vals$UnsortedRep == 1] <- "UR"
table(Vals$SortedParty)

Vals$SameSexM <- NA
Vals$SameSexM[Vals$Q80 == 2] <- 1
Vals$SameSexM[Vals$Q80 == 3] <- 2
Vals$SameSexM[Vals$Q80 == 1] <- 3
# Measure goes from cons to lib

Vals$Abortion <- NA
Vals$Abortion[Vals$Q81 == 1] <- 1
Vals$Abortion[Vals$Q81 == 2] <- 2
Vals$Abortion[Vals$Q81 == 3] <- 3
Vals$Abortion[Vals$Q81 == 5] <- 4
# Q81 == 5 is most liberal, so measure goes from cons to lib

Vals$Blacks <- NA
Vals$Blacks[Vals$Q84 == 4] <- 1 
Vals$Blacks[Vals$Q84 == 3] <- 2
Vals$Blacks[Vals$Q84 == 2] <- 3
Vals$Blacks[Vals$Q84 == 1] <- 4
# Q84 == 4 is most conservative, so measure goes from conservative to liberal

Vals$JobsEnv <- NA
Vals$JobsEnv[Vals$Q85 == 1] <- 4
Vals$JobsEnv[Vals$Q85 == 2] <- 3
Vals$JobsEnv[Vals$Q85 == 3] <- 2
Vals$JobsEnv[Vals$Q85 == 4] <- 1
# Measure goes from cons to lib

Vals$MinWage <- NA
Vals$MinWage[Vals$Q82 == 1] <- 4
Vals$MinWage[Vals$Q82 == 2] <- 3
Vals$MinWage[Vals$Q82 == 3] <- 2
Vals$MinWage[Vals$Q82 == 4] <- 1
# measure goes from cons to liberal

Vals$Female <- NA
Vals$Female[Vals$Q46 == 1] <- 0
Vals$Female[Vals$Q46 == 2] <- 1

Vals$Feminine <- Vals$Q68

Vals$Income <- Vals$Q49 - 1

Vals$Educ <- Vals$Q50

Vals$Educ2 <- NA
Vals$Educ2[Vals$Q50 == 1] <- 0 # Less than HS
Vals$Educ2[Vals$Q50 == 2] <- 1 # HS Diploma
Vals$Educ2[Vals$Q50 == 3] <- 2 # Some College
Vals$Educ2[Vals$Q50 == 4] <- 2 # 2-year Degree
Vals$Educ2[Vals$Q50 == 5] <- 3 # 4-year Degree
Vals$Educ2[Vals$Q50 == 6] <- 4 # Beyond 4-year degree
Vals$Educ2[Vals$Q50 == 7] <- 4 # Beyond 4-year degree

Vals$Relig <- 6 - Vals$Q52

Vals$Age <- Vals$Q87 - 1

Vals$Nonwhite <- ifelse(Vals$Q47 == 1, 0, 1)
table(Vals$Nonwhite)

Vals$Sorted <- ifelse(Vals$SortedDem == 1 |
                        Vals$SortedRep == 1, 1, 0)

Vals$RankEq1 <- Vals$RankEq/4
Vals$RankES1 <- Vals$RankES/4
Vals$RankFr1 <- Vals$RankFree/4
Vals$RankLO1 <- Vals$RankLO/4
Vals$RankMT1 <- Vals$RankMT/4

Vals$JobsEnv <- NA
Vals$JobsEnv[Vals$Q85 == 1] <- 4
Vals$JobsEnv[Vals$Q85 == 2] <- 3
Vals$JobsEnv[Vals$Q85 == 3] <- 2
Vals$JobsEnv[Vals$Q85 == 4] <- 1
# Measure goes from cons to lib

Vals$MinWage <- NA
Vals$MinWage[Vals$Q82 == 1] <- 4
Vals$MinWage[Vals$Q82 == 2] <- 3
Vals$MinWage[Vals$Q82 == 3] <- 2
Vals$MinWage[Vals$Q82 == 4] <- 1
# measure goes from cons to liberal

Vals$EconIssues <- (Vals$JobsEnv + Vals$MinWage) - 2
table(Vals$EconIssues)
Vals$EconIssues1 <- Vals$EconIssues/6
table(Vals$EconIssues1)
# High numbers mean lib

### ECON ISSUES REG. ###

OLS7 <- lm(EconIssues1 ~ RankEq1*Sorted + RankES1*Sorted + 
             Female +
             Educ2 + Relig + Age + Nonwhite + Income,
           data = Vals)
summary(OLS7)

MarginsOLS7 <- margins(OLS7, at = list(Sorted = c(0, 1)), 
                       variables = c("RankEq1", "RankES1"))
summary(MarginsOLS7)

### MFX Graph ###

seq <- seq(1, 6, 1)
# Sorted, unsorted, econ, sorted, unsorted, equality
mfx <- c(.1488, .0338, -9, .3016, .0795, -9)
ll <- c(.0943, -.0335, .000001, .2471, .0143, .000001)
ul <- c(.2033, .1012, .000001, .3561, .1446, .000001)

new.data <- data.frame(seq, mfx, ll, ul)

library(lattice)

dotplot(seq ~ mfx, data = new.data,
        aspect = 1, 
        xlim = c(-.49, .49),
        xlab = "Marginal Effect (OLS)",
        ylab = "",
        scales = list(y = list(tick.number = 6,
                               at = c(1, 2, 3, 4, 5, 6),
                               labels = c("Sorted", "Unsorted", "Econ. Sec.:    ",
                                          "Sorted", "Unsorted", "Equality:    "))),
        panel = function(x, y, subscripts){
          panel.dotplot(x[x > -1], y[x > -1],
                        pch = 19, col = "black", lty = 3)
          panel.abline(v = 0, col = "gray", lty = 2, lwd = 1)
          panel.segments(new.data$ll[subscripts], as.numeric(y),
                         new.data$ul[subscripts], as.numeric(y), lty = 1, col = "black", lwd = 2)} )

##

Vals$SameSexM <- NA
Vals$SameSexM[Vals$Q80 == 2] <- 1
Vals$SameSexM[Vals$Q80 == 3] <- 2
Vals$SameSexM[Vals$Q80 == 1] <- 3
# Measure goes from cons to lib

Vals$Abortion <- NA
Vals$Abortion[Vals$Q81 == 1] <- 1
Vals$Abortion[Vals$Q81 == 2] <- 2
Vals$Abortion[Vals$Q81 == 3] <- 3
Vals$Abortion[Vals$Q81 == 5] <- 4
# Q81 == 5 is most liberal, so measure goes from cons to lib

Vals$Blacks <- NA
Vals$Blacks[Vals$Q84 == 4] <- 1 
Vals$Blacks[Vals$Q84 == 3] <- 2
Vals$Blacks[Vals$Q84 == 2] <- 3
Vals$Blacks[Vals$Q84 == 1] <- 4
# Q84 == 4 is most conservative, so measure goes from conservative to liberal

Vals$SocIssues <- (Vals$SameSexM + Vals$Abortion +
                     Vals$Blacks) - 3
table(Vals$SocIssues)
Vals$SocIssues1 <- Vals$SocIssues/8
table(Vals$SocIssues1)

# High numbers mean liberal

## SOC ISSUES REG ##

OLS8 <- lm(SocIssues1 ~ RankEq1*Sorted + RankLO1*Sorted +
             RankMT1*Sorted + Female +
             Educ2 + Relig + Age + Nonwhite + Income,
           data = Vals)
summary(OLS8)

MarginsOLS8 <- margins(OLS8, at = list(Sorted = c(0, 1)), 
                       variables = c("RankEq1", "RankLO1", "RankMT1"))
summary(MarginsOLS8)

### MFX Graph ###

seq <- seq(1, 9, 1)
# sorted, unsorted, MT, sorted, unsorted, LO, sorted, unsorted, EQ
mfx <- c(-.1443, -.0769, -9, -.1442, -.0462, -9, .2331, .0803, -9)
ll <- c(-.2040, -.1462, .000001, -.2082, -.1218, .000001, .1655, .0036, .00001)
ul <- c(-.0845, -.0077, .000001, -.0802, .0293, .000001, .3007, .1569, .000001)

new.data <- data.frame(seq, mfx, ll, ul)

library(lattice)

dotplot(seq ~ mfx, data = new.data,
        aspect = 1, 
        xlim = c(-.49, .49),
        xlab = "Marginal Effect (OLS)",
        ylab = "",
        scales = list(y = list(tick.number = 6,
                               at = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                               labels = c("Sorted", "Unsorted", "Moral Trad:    ",
                                          "Sorted", "Unsorted", "Law & Ord.:    ",
                                          "Sorted", "Unsorted", "Equality:    "))),
        panel = function(x, y, subscripts){
          panel.dotplot(x[x > -1], y[x > -1],
                        pch = 19, col = "black", lty = 3)
          panel.abline(v = 0, col = "gray", lty = 2, lwd = 1)
          panel.segments(new.data$ll[subscripts], as.numeric(y),
                         new.data$ul[subscripts], as.numeric(y), lty = 1, col = "black", lwd = 2)} )

###

Vals$VoteTrump <- NA
Vals$VoteTrump[Vals$Q41 == 1] <- 0
Vals$VoteTrump[Vals$Q41 == 2] <- 1
Vals$VoteTrump[Vals$Q41 == 3] <- 0
table(Vals$VoteTrump)

######################
### TRUMP VOTE REG ###
######################

GLM1 <- glm(VoteTrump ~ RankEq1*Sorted + RankMT1*Sorted + Female + Educ2 +
              Relig + Age + Nonwhite + Income + RankLO1*Sorted, data = Vals,
            family = binomial(link="logit"))
summary(GLM1)

MarginsGLM1 <- margins(GLM1, at = list(Sorted = c(0, 1)), 
                       variables = c("RankEq1", "RankLO1", "RankMT1"),
                       type = "response")
summary(MarginsGLM1)

### MFX Graph ###

seq <- seq(1, 9, 1)
# sorted, unsorted, MT, sorted, unsorted, LO, sorted, unsorted, EQ
mfx <- c(.1554, -.0708, -9, .2382, -.1471, -9, -.2338, -.2093, -9)
ll <- c(.0460, -.2249, .000001, .1201, -.3160, .000001, -.3533, -.3776, .000001)
ul <- c(.2649, .0834, .000001, .3563, .0218, .000001, -.1142, -.0410, .000001)

new.data <- data.frame(seq, mfx, ll, ul)

library(lattice)

dotplot(seq ~ mfx, data = new.data,
        aspect = 1, 
        xlim = c(-.59, .59),
        xlab = "Marginal Effect (Logit)",
        ylab = "",
        scales = list(y = list(tick.number = 6,
                               at = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
                               labels = c("Sorted", "Unsorted", "Moral Trad:    ",
                                          "Sorted", "Unsorted", "Law & Ord.:    ",
                                          "Sorted", "Unsorted", "Equality:    "))),
        panel = function(x, y, subscripts){
          panel.dotplot(x[x > -1], y[x > -1],
                        pch = 19, col = "black", lty = 3)
          panel.abline(v = 0, col = "gray", lty = 2, lwd = 1)
          panel.segments(new.data$ll[subscripts], as.numeric(y),
                         new.data$ul[subscripts], as.numeric(y), lty = 1, col = "black", lwd = 2)} )
